﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ToolTilePanel : MonoBehaviour
{
	private float alpha = 0.0f;
	private float alphaSpeed = 2.0f;

	private Text toolTilePanel;
	private Text toolTileText;
	private CanvasGroup cg;

	// Use this for initialization
	void Start ()
	{
		toolTilePanel = this.transform.GetComponent<Text>();
		cg = this.transform.GetComponent<CanvasGroup>();
		toolTileText = this.transform.GetChild(1).GetComponent<Text>();
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (alpha != cg.alpha)
		{
			cg.alpha = Mathf.Lerp(cg.alpha,alpha,alphaSpeed * Time.deltaTime);
			if (Mathf.Abs(alpha-cg.alpha)<=0.01)
			{
				cg.alpha = alpha;
			}
		}
	}


	public void ShowPanel(string str = " ")
	{

		//为什么要站在ToolTilePanel上设置两个文本对象？
		//第一个文本对象设置为父节点，它的作用是随文本的长宽来变化背景图片的大小
		//第二个文本对象设置为子节点，它的作用是显示文本（由于背景图片吧文本遮挡住了）
		toolTilePanel.text = str;
		toolTileText.text = str;

		alpha = 1;
	}

	public void HidePanel()
	{
		alpha = 0;
	}


	public void SetLocalPosition(Vector3 point)
	{
		//设置ToolTilePanel相对于父节点的坐标
		this.transform.localPosition = point;
	}
}
